import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import { dirname } from 'node:path'

export function readJSON<T = any>(path: string): T {
  return existsSync(path) ? JSON.parse(readFileSync(path, 'utf-8')) : ({} as T)
}

export function writeJSON(path: string, data: unknown): void {
  mkdirSync(dirname(path), { recursive: true })
  writeFileSync(path, JSON.stringify(data, null, 2))
}
